Skip to content

CodeEditor

Base classes

Name Children Inherits
PlainTextEdit
prettyqt.widgets.plaintextedit
Widget that is used to edit and display plain text.

â‹” Inheritance diagram

graph TD
  4224319152["custom_widgets.CodeEditor"]
  3482197220["widgets.PlainTextEdit"]
  82444806["widgets.PlainTextEditMixin"]
  1259044718["widgets.AbstractScrollAreaMixin"]
  1689631995["widgets.FrameMixin"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  2639575484["QtWidgets.QPlainTextEdit"]
  1459025998["QtWidgets.QAbstractScrollArea"]
  3822057598["QtWidgets.QFrame"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  3482197220 --> 4224319152
  82444806 --> 3482197220
  1259044718 --> 82444806
  1689631995 --> 1259044718
  829357528 --> 1689631995
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  2751680898 --> 1689631995
  2751680898 --> 1259044718
  2751680898 --> 82444806
  2639575484 --> 3482197220
  1459025998 --> 2639575484
  3822057598 --> 1459025998
  3620195106 --> 3822057598
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: PlainTextEdit

Code editor with line numbers, syntax highlighting and line markers.

Line markers (errors / warnings / infos) tint the line and its number; hovering the line number or the line shows the marker messages as tooltip. set_text() keeps cursor and scroll position (and the undo history).

Source code in prettyqt/custom_widgets/codeeditor.py
class CodeEditor(widgets.PlainTextEdit):
    """Code editor with line numbers, syntax highlighting and line markers.

    Line markers (errors / warnings / infos) tint the line and its number;
    hovering the line number or the line shows the marker messages as tooltip.
    `set_text()` keeps cursor and scroll position (and the undo history).
    """

    def __init__(self, language: str = "python", **kwargs):
        super().__init__(**kwargs)
        self._markers: dict[int, list[LineMarker]] = {}
        self.line_area = LineNumberArea(self)
        self.blockCountChanged.connect(self.update_line_area_width)
        self.updateRequest.connect(self.update_line_area)
        self.set_font(gui.Font.mono(as_qt=True))
        self.update_line_area_width(0)
        self.set_current_line_color(gui.Color(128, 128, 128, 20))
        self.set_syntaxhighlighter(language)

    #     self.delimiter = "    "
    #     self.completion_state = 0
    #     self.completing = False
    #     self.cursorPositionChanged.connect(self.reset_completion)

    # def keyPressEvent(self, event):
    #     match event.key():
    #         case constants.Key.Key_Backtab if self.textCursor().hasSelection():
    #             start_cursor = self.get_textCursor()
    #             with start_cursor.edit_block():
    #                 start_pos = start_cursor.selectionStart()
    #                 start_cursor.setPosition(start_pos)
    #                 start_cursor.move_position("start_of_line")
    #                 start_cursor.clearSelection()
    #                 end_cursor = self.get_text_cursor()
    #                 end_pos = end_cursor.selectionEnd()
    #                 end_cursor.setPosition(end_pos)
    #                 end_cursor.move_position("start_of_line")
    #                 delimit_len = len(self.delimiter)
    #                 while start_cursor.anchor() != end_cursor.position():
    #                     start_cursor.move_position(
    #                         "next_character", mode="keep", n=delimit_len
    #                     )
    #                     if start_cursor.selectedText() == self.delimiter:
    #                         start_cursor.removeSelectedText()
    #                     start_cursor.move_position("next_block")
    #                 start_cursor.move_position(
    #                     "next_character", mode="keep", n=delimit_len
    #                 )
    #                 if start_cursor.selectedText() == self.delimiter:
    #                     start_cursor.removeSelectedText()
    #         case constants.Key.Key_Tab if self.textCursor().hasSelection():
    #             start_cursor = self.get_text_cursor()
    #             with start_cursor.edit_block():
    #                 start_pos = start_cursor.selectionStart()
    #                 start_cursor.setPosition(start_pos)
    #                 start_cursor.move_position("start_of_line")
    #                 end_cursor = self.get_text_cursor()
    #                 end_pos = end_cursor.selectionEnd()
    #                 end_cursor.setPosition(end_pos)
    #                 end_cursor.move_position("start_of_line")
    #                 while start_cursor.position() != end_cursor.position():
    #                     start_cursor.insertText(self.delimiter)
    #                     start_cursor.move_position("next_block")
    #                 start_cursor.insertText(self.delimiter)
    #         case constants.Key.Key_Escape if self.completion_state > 0:
    #             self.completion_state = 0
    #             cursor = self.get_text_cursor()
    #             with cursor.edit_block():
    #                 self.selecter.replace_block_at_cursor(self._orig_text)
    #             self._orig_text == None
    #         case constants.Key.Key_Tab:
    #             if self.is_start():
    #                 self.textCursor().insertText(self.delimiter)
    #             else:
    #                 cursor = self.get_text_cursor()
    #                 with cursor.edit_block():
    #                     self.completing = True
    #                     if self.completion_state == 0:
    #                         self._orig_text = self.textCursor().block().text()
    #                     if self.completion_state > 0:
    #                         self.selecter.replace_block_at_cursor(self._orig_text)
    #                     new_text = self.completer.complete(
    #                         self._orig_text, self.completion_state
    #                     )
    #                     if new_text:
    #                         if new_text.find("(") > 0:
    #                             new_text = new_text[0 : new_text.find("(") + 1]
    #                         self.completion_state += 1
    #                         self.selecter.replace_block_at_cursor(new_text)
    #                     else:
    #                         self.completion_state = 0
    #                         self.selecter.replace_block_at_cursor(self._orig_text)
    #                         self._orig_text == None
    #                 self.completing = False
    #         case _:
    #             return super().keyPressEvent(event)

    # def reset_completion(self):
    #     if not self.completing:
    #         self.completion_state = 0

    # def is_start(self) -> bool:
    #     temp_cursor = self.textCursor()
    #     if temp_cursor.positionInBlock() == 0:
    #         return True
    #     start_text = temp_cursor.block().text()[0 : temp_cursor.positionInBlock()]
    #     delim = set(self.delimiter)
    #     return set(start_text) - delim == set()

    def set_syntaxhighlighter(
        self,
        syntax: str | gui.QSyntaxHighlighter,
        style: syntaxhighlighters.pygmentshighlighter.StyleStr | None = None,
    ):
        """Set the highlighter; the previous one is detached from the document."""
        old = getattr(self, "_hl", None)
        if old is not None and old is not syntax:
            old.setDocument(None)
            old.deleteLater()
        super().set_syntaxhighlighter(syntax, style)

    def set_text(self, text: str, *, keep_position: bool = True):
        """Set the text.

        Args:
            text: new text
            keep_position: keep cursor position, selection and scroll position, and
                           make the change undoable (instead of resetting the document)
        """
        if not keep_position:
            self.setPlainText(text)
            return
        if text == self.toPlainText():
            return
        cursor = self.textCursor()
        anchor, position = cursor.anchor(), cursor.position()
        h_value = self.horizontalScrollBar().value()
        v_value = self.verticalScrollBar().value()
        edit_cursor = gui.QTextCursor(self.document())
        edit_cursor.beginEditBlock()
        edit_cursor.select(gui.QTextCursor.SelectionType.Document)
        edit_cursor.insertText(text)
        edit_cursor.endEditBlock()
        last = max(len(text), 0)
        cursor = self.textCursor()
        cursor.setPosition(min(anchor, last))
        cursor.setPosition(min(position, last), gui.QTextCursor.MoveMode.KeepAnchor)
        self.setTextCursor(cursor)
        self.verticalScrollBar().setValue(v_value)
        self.horizontalScrollBar().setValue(h_value)

    def set_value(self, value: str):
        self.set_text(value)

    # --- line markers -----------------------------------------------------

    def add_line_marker(self, line: int, message: str, kind: MarkerKindStr = "error"):
        """Attach a message to a line (1-based line number)."""
        if kind not in MARKER_COLORS:
            msg = f"Invalid marker kind {kind!r}"
            raise ValueError(msg)
        self._markers.setdefault(line, []).append(LineMarker(line, message, kind))
        self._update_markers()

    def set_line_markers(
        self,
        markers: Mapping[int, str] | Iterable[LineMarker | tuple[int, str]],
        kind: MarkerKindStr = "error",
    ):
        """Replace all markers.

        Args:
            markers: {line: message}, LineMarkers or (line, message) tuples
            kind: kind for markers given as mapping / tuples
        """
        self._markers.clear()
        if isinstance(markers, Mapping):
            items: Iterable[LineMarker | tuple[int, str]] = cast(
                "Mapping[int, str]", markers
            ).items()
        else:
            items = markers
        for item in items:
            if isinstance(item, LineMarker):
                marker = item
            else:
                marker = LineMarker(item[0], item[1], kind)
            self._markers.setdefault(marker.line, []).append(marker)
        self._update_markers()

    def clear_line_markers(self, line: int | None = None):
        """Remove the markers of a line, or all markers."""
        if line is None:
            self._markers.clear()
        else:
            self._markers.pop(line, None)
        self._update_markers()

    def get_line_markers(self) -> list[LineMarker]:
        return [m for line in sorted(self._markers) for m in self._markers[line]]

    def get_line_marker_text(self, line: int) -> str:
        """Return the messages of a line, one per row ("" if none)."""
        return "\n".join(m.message for m in self._markers.get(line, []))

    def _marker_color(self, line: int, alpha: int) -> gui.Color | None:
        if not (markers := self._markers.get(line)):
            return None
        kind = min((m.kind for m in markers), key=list(MARKER_COLORS).index)
        return gui.Color(*MARKER_COLORS[kind], alpha)

    def _update_markers(self):
        selections = []
        doc = self.document()
        for line in sorted(self._markers):
            block = doc.findBlockByNumber(line - 1)
            if not block.isValid():
                continue
            selection = widgets.QTextEdit.ExtraSelection()
            selection.format.setBackground(self._marker_color(line, 50))
            prop = gui.QTextFormat.Property.FullWidthSelection
            selection.format.setProperty(prop, True)
            selection.cursor = gui.QTextCursor(block)
            selections.append(selection)
        self.setExtraSelections(selections)
        self.line_area.update()

    def _line_at(self, y: int) -> int | None:
        block = self.firstVisibleBlock()
        top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top()
        while block.isValid():
            bottom = top + self.blockBoundingRect(block).height()
            if top <= y < bottom:
                return block.blockNumber() + 1
            top = bottom
            block = block.next()
        return None

    def show_marker_tooltip(self, pos: core.QPoint, global_pos: core.QPoint) -> bool:
        line = self._line_at(pos.y())
        if line is None or not (text := self.get_line_marker_text(line)):
            widgets.ToolTip.hideText()
            return False
        widgets.ToolTip.showText(global_pos, text)
        return True

    def viewportEvent(self, event):
        if event.type() == core.QEvent.Type.ToolTip:
            self.show_marker_tooltip(event.pos(), event.globalPos())
            return True
        return super().viewportEvent(event)

    def resizeEvent(self, event):
        super().resizeEvent(event)

        cr = self.contentsRect()
        rect = core.Rect(cr.left(), cr.top(), self.line_area_width(), cr.height())
        self.line_area.setGeometry(rect)

    def line_area_width(self) -> int:
        digits = max(2, len(str(self.blockCount())))
        return 10 + self.fontMetrics().horizontalAdvance("9") * digits

    def update_line_area_width(self, _):
        self.setViewportMargins(self.line_area_width(), 0, 0, 0)

    def update_line_area(self, rect: core.QRect, dy: int):
        if dy:
            self.line_area.scroll(0, dy)
        else:
            self.line_area.update(0, rect.y(), self.line_area.width(), rect.height())

        if rect.contains(self.viewport().rect()):
            self.update_line_area_width(0)

    def line_area_paintevent(self, event):
        with gui.Painter(self.line_area) as painter:
            painter.fill_rect(event.rect(), "lightgray")

            block = self.firstVisibleBlock()
            block_number = block.blockNumber()
            top = self.blockBoundingGeometry(block).translated(self.contentOffset()).top()
            bottom = top + self.blockBoundingRect(block).height()
            width = self.line_area.width()
            height = self.fontMetrics().height()
            painter.set_color("black")
            while block.isValid() and (top <= event.rect().bottom()):
                if block.isVisible() and (bottom >= event.rect().top()):
                    number = str(block_number + 1)
                    if color := self._marker_color(block_number + 1, 160):
                        painter.fill_rect(
                            core.QRectF(0, top, width, bottom - top).toRect(), color
                        )
                    painter.drawText(
                        0, int(top), width - 4, height, constants.ALIGN_RIGHT, number
                    )
                block = block.next()
                top = bottom
                bottom = top + self.blockBoundingRect(block).height()
                block_number += 1

add_line_marker(line: int, message: str, kind: MarkerKindStr = 'error')

Attach a message to a line (1-based line number).

Source code in prettyqt/custom_widgets/codeeditor.py
def add_line_marker(self, line: int, message: str, kind: MarkerKindStr = "error"):
    """Attach a message to a line (1-based line number)."""
    if kind not in MARKER_COLORS:
        msg = f"Invalid marker kind {kind!r}"
        raise ValueError(msg)
    self._markers.setdefault(line, []).append(LineMarker(line, message, kind))
    self._update_markers()

clear_line_markers(line: int | None = None)

Remove the markers of a line, or all markers.

Source code in prettyqt/custom_widgets/codeeditor.py
def clear_line_markers(self, line: int | None = None):
    """Remove the markers of a line, or all markers."""
    if line is None:
        self._markers.clear()
    else:
        self._markers.pop(line, None)
    self._update_markers()

get_line_marker_text(line: int) -> str

Return the messages of a line, one per row ("" if none).

Source code in prettyqt/custom_widgets/codeeditor.py
def get_line_marker_text(self, line: int) -> str:
    """Return the messages of a line, one per row ("" if none)."""
    return "\n".join(m.message for m in self._markers.get(line, []))

set_line_markers(markers: Mapping[int, str] | Iterable[LineMarker | tuple[int, str]], kind: MarkerKindStr = 'error')

Replace all markers.

Parameters:

Name Type Description Default
markers Mapping[int, str] | Iterable[LineMarker | tuple[int, str]]

{line: message}, LineMarkers or (line, message) tuples

required
kind MarkerKindStr

kind for markers given as mapping / tuples

'error'
Source code in prettyqt/custom_widgets/codeeditor.py
def set_line_markers(
    self,
    markers: Mapping[int, str] | Iterable[LineMarker | tuple[int, str]],
    kind: MarkerKindStr = "error",
):
    """Replace all markers.

    Args:
        markers: {line: message}, LineMarkers or (line, message) tuples
        kind: kind for markers given as mapping / tuples
    """
    self._markers.clear()
    if isinstance(markers, Mapping):
        items: Iterable[LineMarker | tuple[int, str]] = cast(
            "Mapping[int, str]", markers
        ).items()
    else:
        items = markers
    for item in items:
        if isinstance(item, LineMarker):
            marker = item
        else:
            marker = LineMarker(item[0], item[1], kind)
        self._markers.setdefault(marker.line, []).append(marker)
    self._update_markers()

set_syntaxhighlighter(syntax: str | gui.QSyntaxHighlighter, style: syntaxhighlighters.pygmentshighlighter.StyleStr | None = None)

Set the highlighter; the previous one is detached from the document.

Source code in prettyqt/custom_widgets/codeeditor.py
def set_syntaxhighlighter(
    self,
    syntax: str | gui.QSyntaxHighlighter,
    style: syntaxhighlighters.pygmentshighlighter.StyleStr | None = None,
):
    """Set the highlighter; the previous one is detached from the document."""
    old = getattr(self, "_hl", None)
    if old is not None and old is not syntax:
        old.setDocument(None)
        old.deleteLater()
    super().set_syntaxhighlighter(syntax, style)

set_text(text: str, *, keep_position: bool = True)

Set the text.

Parameters:

Name Type Description Default
text str

new text

required
keep_position bool

keep cursor position, selection and scroll position, and make the change undoable (instead of resetting the document)

True
Source code in prettyqt/custom_widgets/codeeditor.py
def set_text(self, text: str, *, keep_position: bool = True):
    """Set the text.

    Args:
        text: new text
        keep_position: keep cursor position, selection and scroll position, and
                       make the change undoable (instead of resetting the document)
    """
    if not keep_position:
        self.setPlainText(text)
        return
    if text == self.toPlainText():
        return
    cursor = self.textCursor()
    anchor, position = cursor.anchor(), cursor.position()
    h_value = self.horizontalScrollBar().value()
    v_value = self.verticalScrollBar().value()
    edit_cursor = gui.QTextCursor(self.document())
    edit_cursor.beginEditBlock()
    edit_cursor.select(gui.QTextCursor.SelectionType.Document)
    edit_cursor.insertText(text)
    edit_cursor.endEditBlock()
    last = max(len(text), 0)
    cursor = self.textCursor()
    cursor.setPosition(min(anchor, last))
    cursor.setPosition(min(position, last), gui.QTextCursor.MoveMode.KeepAnchor)
    self.setTextCursor(cursor)
    self.verticalScrollBar().setValue(v_value)
    self.horizontalScrollBar().setValue(h_value)

:fa-brands-github: Show source on GitHub